#include "tomatowidget.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QSettings>
#include "services/window/windowservice.h"
TomatoClock::TomatoClock(int workDuration, QWidget *parent)
    : QWidget(parent), workDuration(workDuration),
      duration(workDuration), elapsedTime(0), isPaused(true), isWorking(false), timer(new QTimer(this))
{
    initUI();
    initTimer();
    resetTimer();
}

void TomatoClock::initUI()
{
    timeLabel = new QLabel(this);
    QFont timeLableFont = timeLabel->font();
    timeLableFont.setPixelSize(38);
    timeLableFont.setWeight(500);
    timeLabel->setFont(timeLableFont);
    timeLabel->setAlignment(Qt::AlignCenter);

    startButton = new QPushButton("开始", this);
    pauseButton = new QPushButton("暂停", this);
    resetButton = new QPushButton("重置", this);

    QVBoxLayout *mainLayout = new QVBoxLayout;
    QHBoxLayout *buttonLayout = new QHBoxLayout;
    buttonLayout->addWidget(startButton);
    buttonLayout->addWidget(pauseButton);
    buttonLayout->addWidget(resetButton);

    mainLayout->addWidget(timeLabel);
    mainLayout->addLayout(buttonLayout);

    setLayout(mainLayout);

    connect(startButton, &QPushButton::clicked, this, &TomatoClock::start);
    connect(pauseButton, &QPushButton::clicked, this, &TomatoClock::togglePause);
    connect(resetButton, &QPushButton::clicked, this, &TomatoClock::reset);
}

void TomatoClock::initTimer()
{
    connect(timer, &QTimer::timeout, this, &TomatoClock::updateTime);
    // 初始时禁用暂停按钮和重置按钮
    pauseButton->setEnabled(false);
    resetButton->setEnabled(false);
}

// 时间更新
void TomatoClock::updateTime()
{
    auto windowService = dpfGetService(dpfservice::WindowService);
    if (!isPaused)
    {
        elapsedTime++;
        QSettings *m_iniFile = new QSettings("src/plugins/myClock/data.ini", QSettings::IniFormat);
        QString today = QDate::currentDate().toString("yyyyMMdd");
        QString today_date = m_iniFile->value("user_base/today_date").toString();
        QStringList usedDays = m_iniFile->value("user_base/dev_date").toStringList();
        QStringList usedTime = m_iniFile->value("user_base/dev_time").toStringList();

        QString todaySecond = usedTime.last();
        int today_second_int_last = todaySecond.toInt();

        QString today_second_str = m_iniFile->value("user_base/stick_second").toString();
        int today_second_int = today_second_str.toInt();
        emit updateDashBoardTime();
        if (today == today_date)
        {
            today_second_int++;
            today_second_int_last++;
            m_iniFile->setValue("user_base/stick_second", today_second_int);
            if (!usedTime.isEmpty()) {
                usedTime[usedTime.size() - 1] = QString::number(today_second_int_last);
            }
            m_iniFile->setValue("user_base/dev_time", usedTime);
        }
        else
        {
            usedDays.append(today);
            usedTime.append(0);
            today_second_int_last=0;
            today_second_int = 0;
            today_second_int++;
            today_second_int_last++;
            m_iniFile->setValue("user_base/dev_date", usedDays);
            m_iniFile->setValue("user_base/today_date", today);
            m_iniFile->setValue("user_base/stick_second", today_second_int);
            if (!usedTime.isEmpty()) {
                usedTime[usedTime.size() - 1] = QString::number(today_second_int_last);
            }
            m_iniFile->setValue("user_base/dev_time", usedTime);
        }

        if (elapsedTime >= duration)
        {
            elapsedTime = 0;
            timeLabel->setText("已完成");
            emit timeUpdated("已完成");
            timer->stop();
            windowService->notify(0, "Healthy Code", "休息时间到啦！", {});
        }
        else
        {
            int remainingTime = duration - elapsedTime;
            timeLabel->setText(QTime(0, 0).addSecs(remainingTime).toString("mm:ss"));
            emit timeUpdated(QTime(0, 0).addSecs(remainingTime).toString("mm:ss"));
        }
    }
}

// 时钟开始
void TomatoClock::start()
{
    if (isPaused)
    {
        isPaused = false;
        timer->start(1000);
        // 点击开始按钮后，禁用开始按钮，启用暂停按钮和重置按钮
        startButton->setEnabled(false);
        pauseButton->setEnabled(true);
        resetButton->setEnabled(true);
        startButton->setText("开始");
    }
}

// 时钟暂停
void TomatoClock::pause()
{
    if (!isPaused)
    {
        isPaused = true;
        emit timeUpdated("已暂停");
        timer->stop();
        pauseButton->setEnabled(false);
        startButton->setEnabled(true);
        startButton->setText("继续");
    }
}

void TomatoClock::togglePause()
{
    if (isPaused)
    {
        start();
    }
    else
    {
        pause();
    }
}

void TomatoClock::reset()
{
    pause();
    elapsedTime = 0;
    duration = workDuration; // 重置为工作时长
    // isWorking = true;
    timeLabel->setText(QTime(0, 0).addSecs(duration).toString("mm:ss")); // 更新显示
    emit timeUpdated(QTime(0, 0).addSecs(duration).toString("mm:ss"));
    resetButton->setEnabled(false);
    pauseButton->setEnabled(false);
    startButton->setEnabled(true);
    startButton->setText("开始");
}

void TomatoClock::resetTimer()
{

    duration = workDuration; // 重置为工作时长
    elapsedTime = 0;

    timeLabel->setText(QTime(0, 0).addSecs(duration).toString("mm:ss"));
}
